package org.jeecg.modules.travel.carpool.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.common.util.UUIDGenerator;

import org.jeecg.modules.travel.carpool.entity.TravelCarpool;
import org.jeecg.modules.travel.carpool.entity.TravelJourney;
import org.jeecg.modules.travel.user.entity.TravelWechatUser;
import org.jeecg.modules.travel.user.entity.TravelDrivers;
import org.jeecg.modules.travel.user.entity.TravelStudents;

import org.jeecg.modules.travel.carpool.mapper.TravelCarpoolMapper;
import org.jeecg.modules.travel.carpool.mapper.TravelJourneyMapper;
import org.jeecg.modules.travel.user.mapper.TravelDriversMapper;
import org.jeecg.modules.travel.user.mapper.TravelStudentsMapper;
import org.jeecg.modules.travel.user.mapper.TravelWechatUserMapper;

import org.jeecg.modules.travel.carpool.service.ITravelCarpoolService;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.datetime.standard.DateTimeContext;
import org.springframework.format.datetime.standard.InstantFormatter;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.apache.commons.beanutils.converters.DateConverter;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.web.bind.annotation.PathVariable;
import org.jeecg.common.util.oConvertUtils;

/**
 * @Description: 单表示例
 * @Author: jeecg-boot
 * @Date:2018-12-29
 * @Version:V2.0
 */
@Slf4j
@Api(tags = "单表DEMO")
@RestController
@RequestMapping("/travel/carpool")
public class TravelCarpoolController extends JeecgController<TravelCarpool, ITravelCarpoolService> {
    @Autowired
    private ITravelCarpoolService travelCarpoolService;
    @Autowired
    private TravelCarpoolMapper travelCarpoolMapper;
    @Autowired
    private TravelDriversMapper travelDriversMapper;
    @Autowired
    private TravelJourneyMapper travelJourneyMapper;
    @Autowired
    private TravelStudentsMapper travelStudentsMapper;
    @Autowired
    private TravelWechatUserMapper travelWechatUserMapper;

    @Autowired
    private RedisUtil redisUtil;


    /**
     * 寻找行程
     * @param pageNo
     * @param pageSize
     * @param type
     * @return
     */
    @ApiOperation(value = "获取TravelCarpool列表", notes = "获取TravelCarpool列表")
    @GetMapping(value = "/find")
    public Result<?> find(@RequestParam(name = "pageNo", defaultValue = "0") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
    @RequestParam(name = "type", defaultValue = "false") Boolean type,
    @RequestParam(name = "start_time", defaultValue = "") String start_time,
    @RequestParam(name = "end_time", defaultValue = "") String end_time) {


        Date st=null, et=null;
	
        if(oConvertUtils.isNotEmpty(start_time)){
            st = DateUtils.parseDatetime(start_time);
        }
        if(oConvertUtils.isNotEmpty(end_time)){
            et = DateUtils.parseDatetime(end_time);
        }

        if(st==null || et==null){
            return Result.error("时间参数错误");
        }

        log.info(start_time);
        log.info(end_time);
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        QueryWrapper<TravelCarpool> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("driver", type);
        queryWrapper.eq("status", 1);
        queryWrapper.between("plan_time", start_time,end_time);
        queryWrapper.orderByDesc("id");
        Page<TravelCarpool> page = new Page<>(pageNo, pageSize);

        IPage<TravelCarpool> pageList = travelCarpoolMapper.selectListByFind(page, queryWrapper);
        
        log.info("查询当前页：" + pageList.getCurrent());
        log.info("查询当前页数量：" + pageList.getSize());
        log.info("查询结果数量：" + pageList.getRecords().size());
        log.info("数据总数：" + pageList.getTotal());
        return Result.OK(pageList);
    }


    /**
     * 发布行程
     * @param data
     * @return
     */
    @PostMapping(value = "/add")
    @AutoLog(value = "发布行程")
    @ApiOperation(value = "发布行程", notes = "发布行程")
    public Result<String> add(@RequestBody JSONObject data) {
        return travelCarpoolService.createTravel(data);
    }

    /**
     * 取消行程
     * @param id
     * @return
     */
    @DeleteMapping(value = "/close")
    @AutoLog(value = "取消行程")
    @ApiOperation(value = "取消行程", notes = "取消行程")
    public Result<String> close(@RequestBody JSONObject data) {
        return travelCarpoolService.closeTravel(data);
    }

    /**
     * 我发布的行程
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @ApiOperation(value = "获取TravelCarpool列表", notes = "获取TravelCarpool列表")
    @GetMapping(value = "/plan_list")
    public Result<?> plan_list(@RequestParam(name = "pageNo", defaultValue = "0") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
        @RequestParam(name = "type", defaultValue = "0") Integer type,
                              HttpServletRequest req) {

        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        QueryWrapper<TravelCarpool> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid", sysUser.getId());

        switch (type) {
            case 1:
                queryWrapper.eq("driver", 1);
                break;
            case 2:
                queryWrapper.eq("driver", 0);
                queryWrapper.and(i->i.isNull("iid").or().eq("iid",""));
                break;
            case 3:
                queryWrapper.eq("driver", 0);
                queryWrapper.and(i->i.isNotNull("iid").or().ne("iid",""));
                break;
        
            default:
                break;
        }

        queryWrapper.orderByDesc("id");
        Page<TravelCarpool> page = new Page<>(pageNo, pageSize);

        IPage<TravelCarpool> pageList = travelCarpoolMapper.selectListByUid(page, queryWrapper);
        
        log.info("查询当前页：" + pageList.getCurrent());
        log.info("查询当前页数量：" + pageList.getSize());
        log.info("查询结果数量：" + pageList.getRecords().size());
        log.info("数据总数：" + pageList.getTotal());
        return Result.OK(pageList);
    }

    /**
     * 我的行程
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @ApiOperation(value = "获取TravelJourney列表", notes = "获取TravelJourney列表")
    @GetMapping(value = "/list")
    public Result<?> list(@RequestParam(name = "pageNo", defaultValue = "0") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
        @RequestParam(name = "type", defaultValue = "0") Integer type,
                              HttpServletRequest req) {

        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        QueryWrapper<TravelJourney> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid", sysUser.getId());
        switch (type) {
            case 1:
                queryWrapper.eq("driver", 1);
                break;
            case 2:
                queryWrapper.eq("driver", 0);
                queryWrapper.and(i->i.isNull("sid").or().eq("sid",""));
                break;
            case 3:
                queryWrapper.eq("driver", 0);
                queryWrapper.and(i->i.isNotNull("sid").or().ne("sid",""));
                break;
            default:
                break;
        }
        queryWrapper.orderByDesc("id");
        Page<TravelJourney> page = new Page<>(pageNo, pageSize);

        IPage<TravelJourney> pageList = travelJourneyMapper.selectListByUid(page, queryWrapper);
        log.info("查询当前页：" + pageList.getCurrent());
        log.info("查询当前页数量：" + pageList.getSize());
        log.info("查询结果数量：" + pageList.getRecords().size());
        log.info("数据总数：" + pageList.getTotal());
        return Result.OK(pageList);
    }


    /**
     * 申请加入行程
     * @param data
     * @return
     */
    @PostMapping(value = "/join")
    @AutoLog(value = "加入行程")
    @ApiOperation(value = "加入行程", notes = "加入行程")
    public Result<String> join(@RequestBody JSONObject data) {
        return travelCarpoolService.joinTravel(data);
    }

    /**
     * 关闭申请加入行程
     * @param data
     * @return
     */
    @DeleteMapping(value = "/close_join")
    @AutoLog(value = "关闭申请加入行程")
    @ApiOperation(value = "关闭申请加入行程", notes = "关闭申请加入行程")
    public Result<String> close_join(@RequestBody JSONObject data) {
        return travelCarpoolService.closeJoinTravel(data);
    }

    /**
     * 审核加入行程
     * @param data
     * @return
     */
    @PutMapping(value = "/check_join")
    @AutoLog(value = "审核加入行程")
    @ApiOperation(value = "审核加入行程", notes = "审核加入行程")
    public Result<String> check_join(@RequestBody JSONObject data) {
        return travelCarpoolService.checkJoinTravel(data);
    }

    /**
     * 开始行程
     * @param data
     * @return
     */
    @PutMapping(value = "/start")
    @AutoLog(value = "开始行程")
    @ApiOperation(value = "开始行程", notes = "开始行程")
    public Result<String> start(@RequestBody JSONObject data) {
        return travelCarpoolService.startTravel(data);
    }

    /**
     * 完成行程
     * @param data
     * @return
     */
    @PutMapping(value = "/finish")
    @AutoLog(value = "完成行程")
    @ApiOperation(value = "完成行程", notes = "完成行程")
    public Result<String> finish(@RequestBody JSONObject data) {
        return travelCarpoolService.finishTravel(data);
    }

}
